//
//  XDServiceTagFlowLayout.m
//  XDance
//
//  Created by Vill on 2023/10/19.
//

#import "XDServiceTagFlowLayout.h"

@interface XDServiceTagFlowLayout ()

@property (nonatomic, strong) NSMutableArray * widthOfColumns;
@property (nonatomic, strong) NSMutableArray * itemsAttributes;

@end

@implementation XDServiceTagFlowLayout

- (void) setNumberOfColumns:(NSInteger)numberOfColumns {
    if (_numberOfColumns != numberOfColumns) {
        _numberOfColumns = numberOfColumns;
        [self invalidateLayout];
    }
}

- (void)setMinimumLineSpacing:(CGFloat)minimumLineSpacing {
    if (_minimumLineSpacing != minimumLineSpacing) {
        _minimumLineSpacing = minimumLineSpacing;
        [self invalidateLayout];
    }
}

- (void)setMinimumInteritemSpacing:(CGFloat)minimumInteritemSpacing {
    if (_minimumInteritemSpacing != minimumInteritemSpacing) {
        _minimumInteritemSpacing = minimumInteritemSpacing;
        [self invalidateLayout];
    }
}
- (void)setSectionInset:(UIEdgeInsets)sectionInset {
    if (!UIEdgeInsetsEqualToEdgeInsets(_sectionInset, sectionInset)) {
        _sectionInset = sectionInset;
        [self invalidateLayout];
    }
}
-(void)prepareLayout {  [super prepareLayout];
    if (self.itemsAttributes) {
        [_itemsAttributes removeAllObjects];
    }else {
        _itemsAttributes = [[NSMutableArray alloc] init];
    }
    if (self.widthOfColumns) {
        [_widthOfColumns removeAllObjects];
    }else {
        _widthOfColumns = [[NSMutableArray alloc] init];
    }
    for (NSInteger i = 0; i < self.numberOfColumns; i++) {
        [_widthOfColumns addObject:@(self.sectionInset.left)];
    }
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    CGFloat totalHeight = self.collectionView.frame.size.height;
    CGFloat validHeight = totalHeight - self.sectionInset.top - self.self.sectionInset.bottom - (self.numberOfColumns-1) * self.minimumInteritemSpacing;
    CGFloat itemHeight = validHeight/self.numberOfColumns;
    CGFloat itemWidth = itemHeight;
    for (NSInteger i = 0; i<count; i++) {
        NSInteger index = [self indexOfShortestColumn];
        CGFloat originY = self.sectionInset.top + index * (itemHeight +self.minimumInteritemSpacing);
        CGFloat originX = [_widthOfColumns[index] floatValue];
        NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        if ([self.delegate respondsToSelector:@selector(waterFlowLayout:widthForItem:)]) {
            itemWidth = [self.delegate waterFlowLayout:self widthForItem:indexPath];
        }
        UICollectionViewLayoutAttributes * attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        attr.frame = CGRectMake(originX, originY, itemWidth, itemHeight);
        [_itemsAttributes addObject:attr];
        _widthOfColumns[index] = @(originX + itemWidth + self.minimumLineSpacing);
    }
    [self.collectionView reloadData];
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSMutableArray * array = [NSMutableArray array];
    for (UICollectionViewLayoutAttributes * attr in _itemsAttributes) {
        if (CGRectIntersectsRect(attr.frame, rect)) {
            [array addObject:attr];
        }
    }
    return array;
}
-(CGSize)collectionViewContentSize {
    CGFloat height = self.collectionView.frame.size.height;
    NSInteger index = [self indexOfLongestColumn];
    CGFloat width = [_widthOfColumns[index] floatValue] + self.sectionInset.right - self.minimumLineSpacing;
    return CGSizeMake(width, height);
}
- (NSInteger) indexOfLongestColumn {
    NSInteger index = 0;
    for (NSInteger i = 0; i<_numberOfColumns; i++) {
        if ([_widthOfColumns[i] floatValue] > [_widthOfColumns[index] floatValue]) {
            index = i;
        }
    }
    return index;
}
- (NSInteger) indexOfShortestColumn {
    NSInteger index = 0;
    for (NSInteger i = 0; i<_numberOfColumns; i++) {
        if ([_widthOfColumns[i] floatValue] < [_widthOfColumns[index] floatValue]) {
            index = i;
        }
    }
    return index;
}
@end
